package server;

import entities.TaskEntity;

import json.SearchResultJson;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Yang Fan on 2017/12/15.
 */
public class RandomExec implements TaskExec {

    //Properties property = new Properties();

    String xmlpath = "";   //从配置文件中读取

    public RandomExec(TaskEntity task) {
        xmlpath = task.getXmlDirectory();
        System.out.print(xmlpath);
    }

    public ArrayList<File> getFiles(String path) throws Exception {
        //目标集合fileList
        ArrayList<File> fileList = new ArrayList<File>();
        File file = new File(path);
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File fileIndex : files) {
                //如果这个文件是目录，则进行递归搜
                if (fileIndex.isDirectory()) {
                    getFiles(fileIndex.getPath());
                } else {
                    //如果文件是普通文件，则将文件句柄放入集合中
                    fileList.add(fileIndex);
                }
            }
        }
        return fileList;
    }

    public List<SearchResultJson> run() throws Exception {

        ArrayList<File> lists = getFiles(xmlpath);

        List<SearchResultJson> res = new ArrayList<>();

        if (lists.size() > 1) {
            SearchResultJson result = new SearchResultJson();
            File file = lists.get(0); //拿到文件 // 只取了一个例子
            String filename = file.getName().substring(0,file.getName().lastIndexOf(".")); //获取文件名字
            result.setVoiceId(filename); //这里从filename中得到VoiceId---> 未完成 从xml文件中解析出来各项

            res.add(result);

            file = lists.get(1);
            filename = file.getName().substring(0,file.getName().lastIndexOf("."));
            result = new SearchResultJson();
            result.setVoiceId(filename);
            res.add(result);

        }


            return res;
        }

}
